package org.streamerstudio.ui.properties;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.streamerstudio.model.gstreamer.registry.GstPlugin;
import org.streamerstudio.model.gstreamer.registry.RegistryPackage.Literals;

public class PluginPropertyComposite extends Composite {
	
	private DataBindingContext m_bindingContext;

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	
	private Text versionText;
	private Text licenseText;
	private Text filenameText;
	private Text sourceText;
	private Label packageLabel;
	private Text packageText;
	private Label originLabel;
	private Text originText;
	private Label isLoadedLabel;

	private GstPlugin gstPlugin;
	private Text isLoadedText;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public PluginPropertyComposite(Composite parent, int style) {
		super(parent, style);

		createContents();
	}
	
	private void createContents() {
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);

		TableWrapLayout tableWrapLayout = new TableWrapLayout();
		tableWrapLayout.numColumns = 4;
		setLayout(tableWrapLayout);
		// -- component creation
		Label versionLabel = toolkit.createLabel(this, "Version", SWT.NONE);
		TableWrapData tableWrapData = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 1);
		tableWrapData.valign = TableWrapData.BOTTOM;
		versionLabel.setLayoutData(tableWrapData);
		// -- component creation
		versionText = toolkit.createText(this, "", SWT.NONE);
		versionText.setText("");
		versionText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		// -- component creation
		Label licenseLabel = toolkit.createLabel(this, "License", SWT.NONE);
		TableWrapData tableWrapData_1 = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 1);
		tableWrapData_1.valign = TableWrapData.BOTTOM;
		licenseLabel.setLayoutData(tableWrapData_1);
		// -- component creation
		licenseText = toolkit.createText(this, "", SWT.NONE);
		licenseText.setText("");
		licenseText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		// -- component creation
		Label filenameLabel = toolkit.createLabel(this, "Filename", SWT.NONE);
		TableWrapData tableWrapData_2 = new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1);
		tableWrapData_2.valign = TableWrapData.BOTTOM;
		filenameLabel.setLayoutData(tableWrapData_2);
		// -- component creation
		filenameText = toolkit.createText(this, "", SWT.NONE);
		filenameText.setText("");
		filenameText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 3));
		// -- component creation
		Label sourceLabel = toolkit.createLabel(this, "Source", SWT.NONE);
		TableWrapData tableWrapData_3 = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 1);
		tableWrapData_3.valign = TableWrapData.BOTTOM;
		sourceLabel.setLayoutData(tableWrapData_3);
		// -- component creation
		sourceText = toolkit.createText(this, "", SWT.NONE);
		sourceText.setText("");
		sourceText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));

		// -- component creation
		packageLabel = toolkit.createLabel(this, "Package", SWT.NONE);
		TableWrapData tableWrapData_4 = new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1);
		tableWrapData_4.valign = TableWrapData.BOTTOM;
		packageLabel.setLayoutData(tableWrapData_4);
		// -- component creation
		packageText = toolkit.createText(this, "", SWT.NONE);
		packageText.setText("");
		packageText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		// -- component creation
		originLabel = toolkit.createLabel(this, "Origin", SWT.NONE);
		TableWrapData tableWrapData_5 = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 1);
		tableWrapData_5.valign = TableWrapData.BOTTOM;
		originLabel.setLayoutData(tableWrapData_5);
		// -- component creation
		originText = toolkit.createText(this, "", SWT.NONE);
		originText.setText("");
		originText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		// -- component creation
		isLoadedLabel = toolkit.createLabel(this, "Loaded", SWT.NONE);
		TableWrapData tableWrapData_6 = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 1);
		tableWrapData_6.valign = TableWrapData.BOTTOM;
		isLoadedLabel.setLayoutData(tableWrapData_6);
		// -- component creation
		isLoadedText = new Text(this, SWT.BORDER);
		isLoadedText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		toolkit.adapt(isLoadedText, true, true);
		m_bindingContext = initDataBindings();
	}
	
	public void setInput(GstPlugin plugin){
		this.gstPlugin = plugin;
		m_bindingContext = initDataBindings();
	}
	
	public void update(){
		//m_bindingContext.updateModels();
		m_bindingContext.updateTargets();
	}
	
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue versionTextObserveTextObserveWidget = SWTObservables.observeText(versionText, SWT.Modify);
		IObservableValue gstPluginVersionObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__VERSION);
		bindingContext.bindValue(versionTextObserveTextObserveWidget, gstPluginVersionObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue licenseTextObserveTextObserveWidget = SWTObservables.observeText(licenseText, SWT.Modify);
		IObservableValue gstPluginLicenseObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__LICENSE);
		bindingContext.bindValue(licenseTextObserveTextObserveWidget, gstPluginLicenseObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue filenameTextObserveTextObserveWidget = SWTObservables.observeText(filenameText, SWT.Modify);
		IObservableValue gstPluginFilenameObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__FILENAME);
		bindingContext.bindValue(filenameTextObserveTextObserveWidget, gstPluginFilenameObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue sourceTextObserveTextObserveWidget = SWTObservables.observeText(sourceText, SWT.Modify);
		IObservableValue gstPluginSourceObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__SOURCE);
		bindingContext.bindValue(sourceTextObserveTextObserveWidget, gstPluginSourceObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue packageTextObserveTextObserveWidget = SWTObservables.observeText(packageText, SWT.Modify);
		IObservableValue gstPluginPackageObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__PACKAGE);
		bindingContext.bindValue(packageTextObserveTextObserveWidget, gstPluginPackageObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue originTextObserveTextObserveWidget = SWTObservables.observeText(originText, SWT.Modify);
		IObservableValue gstPluginOriginObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__ORIGIN);
		bindingContext.bindValue(originTextObserveTextObserveWidget, gstPluginOriginObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue isLoadedTextObserveTextObserveWidget = SWTObservables.observeText(isLoadedText, SWT.Modify);
		IObservableValue gstPluginLoadedObserveValue = EMFObservables.observeValue(gstPlugin, Literals.GST_PLUGIN__LOADED);
		bindingContext.bindValue(isLoadedTextObserveTextObserveWidget, gstPluginLoadedObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		return bindingContext;
	}
}
